package com.liziba.rpc;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @auther LiZiBa
 * @date 2020/10/19 22:22
 * @description:
 **/
public class RpcProxyServer {

    /**
     * BIO BLOCK INPUT/OUTPUT 实现接口发布
     * @param service 服务名称
     * @param port    端口号
     */
    public void publisher(Object service, int port){
        // 创建固定大小线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        // 服务端的吞吐量很低 、线程池提高处理能力
        ServerSocket serverSocket = null;
        try{
            // 不断监听客户端的连接
            while (true) {

                serverSocket = new ServerSocket(port);
                // 等待客户端的连接，如果没有客户端的连接，accept则会阻塞
                Socket accept = serverSocket.accept();
                // 一个Socket对象代表一个客户端连接
                executorService.execute(new ProcessHandlerThread(service, accept));
            }
        }catch (IOException e){

        }
    }
}
